package com.ttbj.util;

import java.security.MessageDigest;
import java.util.UUID;


public class Md5Util {
	public Md5Util() {
	}

	public static String byte2hex(byte[] b) { // 二行制转字符
		String hs = "";
		String stmp = "";
		for (int n = 0; n < b.length; n++) {
			stmp = (Integer.toHexString(b[n] & 0XFF));
			if (stmp.length() == 1) {
                hs = hs + "0" + stmp;
            } else {
                hs = hs + stmp;
            }
		}
		return hs.toUpperCase();
	}

	public static String getMD5(String password) {
		String returnStr = null;
		try {
			MessageDigest alga = MessageDigest.getInstance("MD5");

			alga.update(password.getBytes());
			byte[] digesta = alga.digest();
			returnStr = byte2hex(digesta);
		} catch (Exception e) {
		}
		return returnStr;
	}
	
	public static String getMD5Lower(String password) {
		String returnStr = null;
		try {
			MessageDigest alga = MessageDigest.getInstance("MD5");

			alga.update(password.getBytes());
			byte[] digesta = alga.digest();
			returnStr = byte2hex(digesta);
		} catch (Exception e) {
		}
		return returnStr.toLowerCase();
	}

	public static String encodeMD5(String inStr) {
		
		char[] a = inStr.toCharArray();
		for (int i = 0; i < a.length; i++) {
			a[i] = (char) (a[i] ^ 't');
		}
		String s = new String(a);
		return s;
	}

	public static String decodeMD5(String inStr) {
		char[] a = inStr.toCharArray();
		for (int i = 0; i < a.length; i++) {
			a[i] = (char) (a[i] ^ 't');
		}
		String k = new String(a);
		return k;
	}
	
	public static String encode(String str) {
		return encodeMD5(getMD5(str));
	}
	
	public static String decode(String str) {
		return decodeMD5(str);
	}
	
	public static void main(String args[]) {
//
		Md5Util m = new Md5Util();
//		
//		System.out.println("MD5 Test suite:");
//		System.out.println("MD5原始:admin");
		
//		System.out.println("MD5后:" +m.getMD5("1"));
//		System.out.println("MD5后在加密:" + encode("admin"));
//		System.out.println("MD5后在解密:" + decode("FEFGF2FMC5AC5A5C@GLM@5D1@5LDE27G"));
		
		String prefix = "http://api.jiangzuotong.com/api/v2/room/enter?";
		
		StringBuffer sb = new StringBuffer();
		sb.append("nickname=test&partner=20140804144140&roomId=");
		sb.append("jz6040731974c747098894cd0c15cf4377");
		sb.append("&timestamp=");
		sb.append(System.currentTimeMillis());
		sb.append("&uid=");
		sb.append(UUID.randomUUID().toString().replaceAll("-", ""));
		
		System.out.println(sb.toString() + "4707e1ae0400452d81184f23306d02fe");
		
		String sign = m.getMD5(sb.toString() + "4707e1ae0400452d81184f23306d02fe").toLowerCase();
		System.out.println(sign);
		String url = prefix + sb.toString() + "&sign=" + sign;
		System.out.println(url);
	}

}

